package org.kernely.stream.utils;

import java.util.Date;

import org.kernely.ui.mvp.IKernelyView;
import org.kernely.ui.mvp.KernelyEventSender;

import com.vaadin.terminal.Sizeable;
import com.vaadin.ui.Button;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.Label;
import com.vaadin.ui.Table;
import com.vaadin.ui.themes.Reindeer;

/**
 * This table is a Vaadin table, pre-constructed with Kernely messages columns.
 */
@SuppressWarnings("serial")
public class KernelyMessageTable extends Table implements KernelyEventSender {

	private static final String IMAGE_LITTERAL = "image";
	private static final String MESSAGE_LITTERAL = "message";
	private static final String BUTTON_FAV_LITTERAL = "button_fav";
	private static final String BUTTON_PROFILE_LITTERAL = "button_profile";
	private static final String BUTTON_DEL_LITTERAL = "button_del";
	private static final String DATE_LITTERAL = "date";
	
	private IKernelyView containingView;
	
	/**
	 * Create a table. If the table contain comments, favorites icon is not displayed.
	 * @param isComment Is the table a table containing comments ? If not, it will contain messages.
	 * @param containingView is the view containing the table.
	 */
	public KernelyMessageTable(boolean isComment){
		
		this.containingView = containingView;
		
		this.addStyleName(Reindeer.TABLE_BORDERLESS);

		this.setSizeFull();
		this.setHeight(100, Sizeable.UNITS_PERCENTAGE);

		this.addContainerProperty(IMAGE_LITTERAL, Embedded.class, null);
		this.addContainerProperty(MESSAGE_LITTERAL, Label.class,  null);
		this.addContainerProperty(BUTTON_PROFILE_LITTERAL, Button.class,  null);
		
		if (!isComment){
			this.addContainerProperty(BUTTON_FAV_LITTERAL, Button.class,  null);
		}
		this.addContainerProperty(BUTTON_DEL_LITTERAL, Button.class,  null);
		this.addContainerProperty(DATE_LITTERAL, Date.class, null);
		this.setColumnWidth(IMAGE_LITTERAL, 48);
		this.setColumnWidth(DATE_LITTERAL, 0);

		if (! isComment){
			this.setImmediate(true);
		}
		this.setSelectable(true);
		this.setPageLength(7);

		this.setRowHeaderMode(Table.ROW_HEADER_MODE_ICON_ONLY);
		this.setSortContainerPropertyId(DATE_LITTERAL);
		this.setSortAscending(false);

		this.setColumnHeaderMode(Table.COLUMN_HEADER_MODE_HIDDEN);

		this.setRowHeaderMode(Table.ROW_HEADER_MODE_HIDDEN);

		this.setColumnExpandRatio(MESSAGE_LITTERAL, 18);
		this.setColumnExpandRatio(BUTTON_PROFILE_LITTERAL, 1);
		this.setColumnExpandRatio(BUTTON_FAV_LITTERAL, 1);
		this.setColumnExpandRatio(BUTTON_DEL_LITTERAL, 1);
		this.setColumnWidth(DATE_LITTERAL, 1);
	}



}
